package com.ruoyi.xtgl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.xtgl.domain.Ywjh;
import com.ruoyi.xtgl.mapper.YwjhMapper;
import com.ruoyi.xtgl.service.IYwjhService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * 业务计划Controller
 *
 * @author huacai
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/xtgl/ywjh")
public class YwjhController extends BaseController {
    @Resource
    private IYwjhService ywjhService;

    @Resource
    private YwjhMapper ywjhMapper;

    /**
     * 查询业务计划列表
     */
    @PreAuthorize("@ss.hasPermi('xtgl:ywjh:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ywjh ywjh) {
        startPage();
        List<Ywjh> list = ywjhService.selectYwjhList(ywjh);
        return getDataTable(list);
    }

    /**
     * 查询业务计划列表(用于查询计划类别)
     */
    @GetMapping("/selectYwJh")
    public TableDataInfo selectYwJh(Ywjh ywjh) {
        startPage();
        List<Ywjh> list = ywjhService.selectYwJh(ywjh);
        return getDataTable(list);
    }

    /**
     * 不分页查询业务计划中的加计扣除计划列表
     */
    @GetMapping("/selectBfyYwJhList")
    public AjaxResult selectBfyYwJhList() {
        List<Ywjh> list = ywjhService.selectBfyYwJhList();
        return AjaxResult.success(list);
    }

    /**
     * 导出业务计划列表
     */
    @PreAuthorize("@ss.hasPermi('xtgl:ywjh:export')")
    @Log(title = "业务计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ywjh ywjh) {
        List<Ywjh> list = ywjhService.selectYwjhList(ywjh);
        ExcelUtil<Ywjh> util = new ExcelUtil<Ywjh>(Ywjh.class);
        util.exportExcel(response, list, "业务计划数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Ywjh> util = new ExcelUtil<Ywjh>(Ywjh.class);
        util.importTemplateExcel(response, "业务计划数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "业务计划", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('xtgl:ywjh:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Ywjh> util = new ExcelUtil<Ywjh>(Ywjh.class);
        InputStream inputStream = file.getInputStream();
        List<Ywjh> list = util.importExcel(inputStream);
        inputStream.close();
        int count = ywjhService.batchInsertYwjh(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取业务计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('xtgl:ywjh:query')")
    @GetMapping(value = "/{ywjhId}")
    public AjaxResult getInfo(@PathVariable("ywjhId") String ywjhId) {
        return success(ywjhService.selectYwjhByYwjhId(ywjhId));
    }

    /**
     * 新增业务计划
     */
    @PreAuthorize("@ss.hasPermi('xtgl:ywjh:add')")
    @Log(title = "业务计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult insertYwjh(@RequestBody Ywjh ywjh) {
        return toAjax(ywjhService.insertYwjh(ywjh));
    }

    /**
     * 新增年度业务计划
     */
    @Log(title = "年度业务计划", businessType = BusinessType.INSERT)
    @PostMapping("/insertNdYwjh")
    public AjaxResult insertNdYwjh(@RequestBody Ywjh ywjh) {
        String nd = ywjh.getNd(); //获取年度

        List<Ywjh> ywjhNdList = ywjhMapper.selectYwjhNdList(); //已有的年度
        for (Ywjh ywjhNd : ywjhNdList) {
            String jhNd = ywjhNd.getNd(); //已有年度
            if (ywjh.getNd().equals(jhNd)) {
                return error(ywjh.getNd() + "年度计划已存在！");
            }
        }

        List<Ywjh> ywJhs = Arrays.asList(
                createYwjh(nd, "项目管理", "立项申报", "年项目申报活动", 1),
                createYwjh(nd, "项目管理", "合同编制", "年合同编制活动", 2),
                createYwjh(nd, "项目管理", "执行情况", "年执行情况活动", 3),
                createYwjh(nd, "项目管理", "中间检查", "年中间检查活动", 4),
                createYwjh(nd, "项目管理", "成果鉴定", "年成果鉴定活动", 5),
                createYwjh(nd, "项目管理", "公司科技进步奖", "年科技奖申报", 6),
                createYwjh(nd, "项目管理", "预算申报", "年预算管理活动", 7),
                createYwjh(nd, "科技成果", "企业工法申报", "年企业工法申报", 8),
                createYwjh(nd, "科技成果", "专利申请", "年专利申请", 9),
                createYwjh(nd, "科技成果", "商标申请", "年商标申请", 10),
                createYwjh(nd, "科技成果", "软件著作权", "年著作权申请", 11),
                createYwjh(nd, "专家评审", "立项评审", "年立项评审项目", 12),
                createYwjh(nd, "专家评审", "成果鉴定", "年成果鉴定项目", 13),
                createYwjh(nd, "专家评审", "科技进步奖", "年科技奖评审", 14),
                createYwjh(nd, "专家评审", "企业工法", "年企业工法评审", 15),
                createYwjh(nd, "政策享受", "加计扣除", "年加计扣除活动", 16),
                createYwjh(nd, "平台资质", "高企认定", "年高企认定活动", 17)
        );

        ywJhs.forEach(y -> ywjhService.insertYwjh(y));

        return toAjax(1);
    }

    private Ywjh createYwjh(String nd, String ywSb, String ywLx, String jhLbSuffix, int px) {
        Ywjh ywjh = new Ywjh();
        ywjh.setYwSb(ywSb);
        ywjh.setNd(nd);
        ywjh.setYwLx(ywLx);
        ywjh.setJhLb(nd + jhLbSuffix);
        ywjh.setPx(px);
        return ywjh;
    }

    /**
     * 修改业务计划
     */
    @PreAuthorize("@ss.hasPermi('xtgl:ywjh:edit')")
    @Log(title = "业务计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ywjh ywjh) {
        return toAjax(ywjhService.updateYwjh(ywjh));
    }

    /**
     * 删除业务计划
     */
    @PreAuthorize("@ss.hasPermi('xtgl:ywjh:remove')")
    @Log(title = "业务计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ywjhIds}")
    public AjaxResult remove(@PathVariable String[] ywjhIds) {
        return toAjax(ywjhService.deleteYwjhByYwjhIds(ywjhIds));
    }
}
